home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / lib-tk / FixTk.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  2.2 KB  |  65 lines

  1. """Utility which tries to locate the Tcl/Tk 8.0 DLLs on Windows.
  2.  
  3. This is a no-op on other platforms.
  4. """
  5.  
  6. # Error messages we may spit out
  7.  
  8. NO_TCL_MESSAGE = """\
  9. WHOOPS!  I can't find a Tcl/Tk 8.0 installation anywhere.
  10. Please make sure that Tcl.Tk 8.0 is installed and that the PATH
  11. environment variable is set to include the Tcl/bin directory
  12. (or wherever TK80.DLL and TCL80.DLL are installed).
  13. If you don't know how to fix this, consider searching the Python FAQ
  14. for the error you get; post to the comp.lang.python if all else fails.
  15. Read the source file FixTk.py for details.
  16. """
  17.  
  18. NO_TKINTER_MESSAGE = """\
  19. WHOOPS!  Even though I think I have found a Tcl/Tk 8.0 installation,
  20. I can't seem to import the _tkinter extension module.
  21. I get the following exception:
  22.     ImportError: %s
  23. If you don't know how to fix this, consider searching the Python FAQ
  24. for the error you get; post to the comp.lang.python if all else fails.
  25. Read the source file FixTk.py for details.
  26. """
  27.  
  28. import sys
  29. if sys.platform == "win32":
  30.     try:
  31.         import _tkinter
  32.     except ImportError:
  33.         import os
  34.         try:
  35.             path = os.environ['PATH']
  36.         except KeyError:
  37.             path = ""
  38.         python_exe = sys.executable
  39.         python_dir = os.path.dirname(python_exe)
  40.         program_files = os.path.dirname(python_dir)
  41.         def tclcheck(dir):
  42.             for dll in "tcl80.dll", "tk80.dll", "tclpip80.dll":
  43.                 if not os.path.isfile(os.path.join(dir, dll)):
  44.                     return 0
  45.             return 1
  46.         for tcldir in [program_files, "\\Program files", "\\",
  47.                        "C:\\Program Files", "D:\\Program Files"]:
  48.             tcldir = os.path.join(tcldir, "Tcl", "bin")
  49.             if tclcheck(tcldir):
  50.                 break
  51.         else:
  52.             tcldir = None
  53.         if not tcldir:
  54.             sys.stderr.write(NO_TCL_MESSAGE)
  55.         else:
  56.             if path and path[-1] != os.pathsep:
  57.                 path = path + os.pathsep
  58.             path = path + tcldir
  59.             os.environ["PATH"] = path
  60.             os.putenv("PATH", path)
  61.             try:
  62.                 import _tkinter
  63.             except ImportError, message:
  64.                 sys.stderr.write(NO_TKINTER_MESSAGE % str(message))
  65.